package xps.and.uudaijia.userclient.view.fragment;

import android.graphics.Outline;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import com.jude.utils.JUtils;
import xps.and.uudaijia.userclient.R;

public class TabLayoutFragment extends Fragment {
    TabLayout tabLayout = null;
    int mElevation = JUtils.dip2px(2);
    ViewPager vp = null;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tablayou, container, false);
        tabLayout = (TabLayout) v.findViewById(R.id.tabLayout);

        tabLayout.setClipToOutline(true);
        if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
            tabLayout.setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    tabLayout.setElevation(mElevation);
                    tabLayout.setPadding(0, 0, 0, mElevation);
                    outline.setRect(0, 0, view.getWidth(), view.getHeight() - mElevation);
                }
            });
        }

        return v;
    }

    public void bindWithViewPager(ViewPager vp) {
        this.vp = vp;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (tabLayout != null && vp != null)
            tabLayout.setupWithViewPager(vp);
    }
}
